package ControllerSrevlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import org.apache.commons.io.IOUtils;

/**
 * Servlet implementation class VideoDownload
 */
@WebServlet("/VideoDownload")
public class VideoDownload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VideoDownload() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		String videosrc = request.getParameter("videosrc");
        String filename = videosrc;
        ServletContext servletContext = this.getServletContext();
        String filePath = servletContext.getRealPath("/Video/" + filename);
 
        FileInputStream fileInputStream = new FileInputStream(filePath);
        ServletOutputStream servletOutputStream = response.getOutputStream();
 
        String mimeType = servletContext.getMimeType(filename);
        response.setHeader("content-type", mimeType);
        response.setHeader("content-disposition" ,"attachment;filename="+filename);
        byte[] bytes = new byte[1024]; //设置读取文件时的规模，大小
        int len = 0;
        while ((len = fileInputStream.read(bytes)) != -1) {
            servletOutputStream.write(bytes, 0, len);
        }
        fileInputStream.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
